package LAAMI;

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;

import cern.colt.list.DoubleArrayList;
import cern.jet.stat.Descriptive;

public class FileRead4 {

	static ArrayList<Set> arrSet;

	public static void main(String[] args) {
		for (int sigma = 1; sigma <= 5; sigma += 1) {
			for (int mu = -5; mu <= 5; mu += 2) {
				String inputFN = "output/pjp_" + (double)sigma / 10 + "_" + (double)mu / 10
						+ ".txt";
				String outputFN = "output/" + (double)sigma / 10 + "_" + (double)mu / 10
						+ ".txt";
				String str = fileRead(inputFN);
				str = str.replace('\n', ' ');
				String[] ar = str.split(" ");
				arrSet = new ArrayList<Set>();
				for (int i = 0; i <= ar.length - 3; i += 3) {
					int pat = (int) Integer.valueOf(ar[i]);
					double jud = (double) Double.valueOf(ar[i + 1]);
					double prof = (double) Double.valueOf(ar[i + 2]);
					Set set = new Set(pat, jud, prof);
					arrSet.add(set);
				}

				for (int p = 5; p < 45; p += 4) {
					for (double j = 0.5; j < 1.5; j += 0.1) {
						DoubleArrayList a = new DoubleArrayList();
						for (int i = 0; i < arrSet.size(); i++) {
							Set agent = new Set();
							agent = arrSet.get(i);
							if (agent.getPat() >= p && agent.getPat() < p + 4
									&& agent.getJud() >= j
									&& agent.getJud() < j + 0.1)
								a.add(agent.getProf());
						}
						Set averSet = new Set();
						averSet.setJud(j + 0.05);
						averSet.setPat(p + 2);
						averSet.setProf(Descriptive.mean(a));
						print(averSet, outputFN);
					}
					print(outputFN);
				}
			}
		}
	}

	public static void print(Set a, String filename) {
		try {
			FileWriter fstream = new FileWriter(filename, true);
			BufferedWriter out = new BufferedWriter(fstream);
			String s = a.getPat() + " " + a.getJud() + " " + a.getProf() + "\n";
			out.write(s);
			out.close();
		} catch (IOException e) {
		}
	}

	public static void print(String filename) {
		try {
			FileWriter fstream = new FileWriter(filename, true);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write('\n');
			out.close();
		} catch (IOException e) {
		}
	}

	public static String fileRead(String fileName) {
		File file = new File(fileName);
		int ch;
		StringBuffer strContent = new StringBuffer("");
		FileInputStream fin = null;
		try {
			fin = new FileInputStream(file);
			while ((ch = fin.read()) != -1)
				strContent.append((char) ch);
			fin.close();
		} catch (Exception e) {
			System.out.println(e);
		}
		return strContent.toString();
	}
}